<?php
    defined('BASEPATH') OR exit('No direct script access allowed');
	class Nav extends CI_Controller {
        public $pagename = "nav";
        public function __construct(){
            parent::__construct();
            if(!$this->session->userdata('is_login')){
                redirect("/admin/auth/login");
            }
            $this->load->model("admin/Nav_model");
        }
        public function index(){
            $navs = $this->Nav_model->get_Nav();
            $data = array(
                "navs" => $navs
                );
            $this->load->view("admin/Nav_index",$data);
        }
        public function add(){
            $this->load->library('form_validation');
            $this->form_validation->set_rules('name', '分类', 'required');
            if ($this->form_validation->run() == FALSE){
                $navs = $this->Nav_model->add();
                $nav = new stdClass;
                $nav->name = "";
                    $data = array(
                        "navs" => $navs,
                        "nav" => $nav,
                        "title" => "添加",
                    );
                $this->load->view("admin/Nav_form",$data);
            }else{
                $data = $this->input->post();
                $this->Nav_model->Add_save($data);
                redirect("/admin/nav/index");
            }
        }
        public function edit($id){
            $this->load->library('form_validation');
            $this->form_validation->set_rules('name', '分类', 'required');        
            if ($this->form_validation->run() == FALSE){
                $nav = $this->Nav_model->get_One($id);
                     $data = array(
                        "nav" => $nav,
                        "title" => "修改",
                    );
                $this->load->view("admin/Nav_form",$data);
            }else{
                $data = $this->input->post();
                $this->Nav_model->Edit_save($id,$data);
                redirect("/admin/nav/index");
            }
        }
        public function del($id){
            // $id = $this->uri->segment(4);
          $this->Nav_model->Delete($id);
            redirect("/admin/nav/index");
        }
        public function login(){
            $this->session->sess_destroy();
            redirect("admin/auth/login"); 
        }
	}
?>